package com.fyzk.activity.zhifu.zc;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.zc_new.OldFootballBQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballJQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballR9Activity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballSFCActivity_New;
import com.fyzk.activity.zhifu.BetBaseActivity;
import com.fyzk.bean.ZCGameQueueItem;
import com.fyzk.betcode.AppLotteryCodeListManager;
import com.fyzk.betcode.CodeBase;
import com.fyzk.betcode.CodeFactory;
import com.fyzk.betcode.ZCBetInfo;
import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.ASView;

import java.util.ArrayList;
import java.util.List;

public class ZhiFu_ZC_Activity extends BetBaseActivity implements ZhiFu_ZC_Adapter.ZC_ZhiFu_OnClickBallListerner, ASView
        .OnValueChangedLisener {

    private LinearLayout zhifu_top;

    private ListView listView;
    protected List<ZCGameQueueItem> infors = new ArrayList<ZCGameQueueItem>();
    private ZhiFu_ZC_Adapter adapter;

    private ASView zhuiHao;
    private ASView touBei;

    private ZCBetInfo betInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        betInfo = AppLotteryCodeListManager.getInstance().getZCBetInfoList().get(0);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        infors = AppLotteryCodeListManager.getInstance().getZCBetInfoList().get(0).infors;
        adapter = getZhiFuAdapter();
        adapter.setListener(this);
        if (listView != null) {
            listView.setAdapter(adapter);
        }
        setBtmView();
    }

    protected ZhiFu_ZC_Adapter getZhiFuAdapter() {
        return new ZhiFu_ZC_Adapter(this, infors);
    }

    @Override
    protected void adapterChange() {
        // TODO Auto-generated method stub
        if (adapter != null) {
            adapter.setList(infors);
        }
    }

    @Override
    public void onBackPressed() {
        AppLotteryCodeListManager.betType = AppLotteryCodeListManager.MODIFICATIONCODE;
        finish();
    }

    @Override
    protected void clear() {
        // TODO Auto-generated method stub
//        if (jcInfors.size() > 0) {
//            jcInfors.clear();
//        }
        AppLotteryCodeListManager.betType = AppLotteryCodeListManager.OKTOBACK;
        for (int i = 0; i < infors.size(); i++) {
            ZCGameQueueItem info = infors.get(i);
            info.clearSelStatu();
        }
        adapter.setList(infors);
    }

    @Override
    public int getZhushu() {
        // TODO Auto-generated method stub
//        for (ZCBetInfo info : jcInfors) {
//            amount += info.zhuShu;
//        }
        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();
        for (int i = 0; i < infors.size(); i++) {
            ZCGameQueueItem info = infors.get(i);
            if (info.isSheng() || info.isPing() || info.isFu()) {
                changCi++;
            }

            clicknums.add(info.clickNum);
            dannums.add(false);
        }
        if (changCi >= getMinChangCi()) {
            zhushu = JZBetNumUtil.getZhushu(getMinChangCi(), clicknums, dannums);
        }
        return zhushu;
    }

    protected int getMinChangCi() {
        return 14;
    }

    @Override
    public String getBetCode() {

        CodeBase codeObj = CodeFactory.getCode(lotteryId);
        String zhuMa = codeObj.betCodeZC(infors);

        StringBuffer sb = new StringBuffer();

        sb.append(getWcodeID());
        sb.append("-");
        sb.append(zhuMa);
        sb.append("^");
        return sb.toString();
    }

    protected String getWcodeID() {
        String wcodeID = "";
        if (zhushu > 1) {
            wcodeID = "" + LotteryIDUtils.ZCSFC_LOTTERY_COMMON_FU_INT;
        }else{
            wcodeID = "" + LotteryIDUtils.ZCSFC_LOTTERY_COMMON_DAN_INT;
        }
        return wcodeID;
    }


    @Override
    public String getPeriod() {
        // TODO Auto-generated method stub
        if (betInfo != null) {
            return betInfo.phase;
        }
        return super.getPeriod();
    }

    @Override
    public int getOneAmount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    protected View initCenterView() {
        // TODO Auto-generated method stub
        View view = LayoutInflater.from(this).inflate(
                R.layout.dj_layout_numzhifu, null);
        // 优惠劵
        LinearLayout coupon_ll = view.findViewById(R.id.zhifu_coupon_ll);
        coupon_ll.addView(couponZhifuView);

        view.findViewById(R.id.szc_zhifu_isprizestop_ll).setVisibility(View.GONE);
        zhifu_top = view.findViewById(R.id.szc_zhifu_top);
        zhifu_top.setVisibility(View.GONE);
        listView = view.findViewById(R.id.szc_zhifu_listview);
        listView.setItemsCanFocus(false);
        adapter = new ZhiFu_ZC_Adapter(this, infors);
        adapter.setListener(this);
        listView.setAdapter(adapter);
        zhuiHao = view.findViewById(R.id.szc_zhifu_zhuiqi);
        zhuiHao.setVisibility(View.GONE);
        TextView line_tv = view.findViewById(R.id.zc_fen);
        line_tv.setVisibility(View.GONE);
        touBei = view.findViewById(R.id.szc_zhifu_beishu);
        touBei.setLeftText("投");
        touBei.setRightText("倍");
        touBei.setOnValueChangedLisener(this);
        return view;
    }

    @Override
    public void onClickBall() {
        LogUtil.d("onclick...");
        setBtmView();
    }

    private void fromZhiFuBack(int position) {
        Intent intent;
        switch (lotteryId) {
            case LotteryIDUtils.ZCSFC_LOTTERY_ID_INT:
                intent = new Intent(getContext(), OldFootballSFCActivity_New.class);
                break;
            case LotteryIDUtils.ZCRXJ_LOTTERY_ID_INT:
                intent = new Intent(getContext(), OldFootballR9Activity_New.class);
                break;
            case LotteryIDUtils.ZCBQC_LOTTERY_ID_INT:
                intent = new Intent(getContext(), OldFootballBQCActivity_New.class);
                break;
            case LotteryIDUtils.ZCJQC_LOTTERY_ID_INT:
                intent = new Intent(getContext(), OldFootballJQCActivity_New.class);
                break;
            default:
                intent = null;
                break;
        }
        if (intent != null) {
            intent.putExtra("position", position);
            intent.putExtra("isZhiFuBack", true);
            startActivity(intent);
        }
    }


    @Override
    public void onValueChanged(ASView view, int value) {
        beishu = value;
//				if (beishu < 1) {
//					beishu = 1;
//				}
        setBtmView();
    }
}
